package com.itdct.onflow.core.config

import com.itdct.onflow.core.config.onflow.CorsConfig
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

/**
 * @author Zhouwx
 * @date 2025/10/21 14:28:58
 * @version 1.0
 * @description
 */
@ConditionalOnProperty(prefix = "onflow.cors", name = ["enable"], havingValue = "true")
@Configuration
class GlobalCorsConfig(
    val corsConfig: CorsConfig
) : WebMvcConfigurer {
    override fun addCorsMappings(registry: CorsRegistry) {
        registry.addMapping(corsConfig.pathPattern)
            .allowedOrigins(*corsConfig.allowOrigins.toTypedArray())
            .allowedMethods(*corsConfig.allowMethods.toTypedArray())
            .allowedHeaders(*corsConfig.allowHeaders.toTypedArray())
            .allowCredentials(corsConfig.allowCredentials)
            .maxAge(corsConfig.maxAge)
    }
}